package RPPM.servlet;


import RPPM.dao.impl.StuDaoImpl;
import RPPM.domain.*;
import RPPM.service.Impl.StuServiceImpl;
import RPPM.service.StuService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
/*
* 1.发送申请表
* 2.保存缴费记录
* 3.保存评价表
* */
@WebServlet("/stuservlet/*")
public class StuServlet extends BaseServlet {
    public void SaveApplication(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("SaveApplication");
        String count = request.getParameter("count");
        String feedback = request.getParameter("feedback");
        System.out.println(count+" "+feedback);

        int stu_id = (int) request.getSession().getAttribute("stuid");

        System.out.println(stu_id);

        System.out.println("session" + stu_id);

        StuService stuService = new StuServiceImpl();
        List<Map<String, Object>> courses = stuService.findCoursesNotAdvice(stu_id);

        Map<String, Object> advice = courses.get(Integer.parseInt(count)-1);

        boolean flag = stuService.saveCourseAdvice(stu_id, advice, feedback);

        ResultInfo info = new ResultInfo();
        info.setFlag(flag);
        try {
            writeJsonValue(response, info);
            System.out.println("json回写成功true");
        } catch (IOException e) {
            System.out.println("json回写失败");
        }

    }
    public void savePayment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("savePayment");

        int stu_id = (int) request.getSession().getAttribute("stuid");
        String tmp = request.getParameter("count");
        System.out.println(stu_id+" "+tmp);

        StuService stuService = new StuServiceImpl();

        List<Map<String, Object>> courses = stuService.findCoursesNotPaid(stu_id);
        System.out.println(courses);
        Map<String, Object> pay = courses.get(Integer.parseInt(tmp)-1);
        System.out.println(pay.get("Cou_time").getClass());
        System.out.println(pay);
        System.out.println(pay.get("Cou_payment").getClass());
        System.out.println(pay.get("Cou_name").getClass());
        System.out.println(pay.get("Cou_time").getClass());

        Payment payment = new Payment(Integer.parseInt((String) pay.get("Cou_payment")), (String)pay.get("Cou_name"),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(pay.get("Cou_time")) );
        boolean flag = stuService.savePayment(payment, stu_id);

        ResultInfo info = new ResultInfo();
        if (flag) {
            info.setFlag(true);
        } else {
            info.setFlag(false);
            info.setErrorMsg("保存缴费记录失败！");
        }
        // 将info序列化为json，并写回到客户端
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(info);
        // 将json写回客户端
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);
    }
    public void savePaymentEX(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("savePaymentEX");

        int stu_id = (int) request.getSession().getAttribute("stuid");
        String count = request.getParameter("count");
        String searchText = (String) request.getSession().getAttribute("searchText");
        System.out.println(stu_id+" "+count);

        StuService stuService = new StuServiceImpl();

        List<Map<String, Object>> courses = stuService.findCoursesNotPaidEX(stu_id,searchText);
        System.out.println(courses);
        Map<String, Object> pay = courses.get(Integer.parseInt(count)-1);
        System.out.println(pay.get("Cou_time").getClass());
        System.out.println(pay);
        System.out.println(pay.get("Cou_payment").getClass());
        System.out.println(pay.get("Cou_name").getClass());
        System.out.println(pay.get("Cou_time").getClass());

        Payment payment = new Payment(Integer.parseInt((String) pay.get("Cou_payment")), (String)pay.get("Cou_name"),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(pay.get("Cou_time")) );
        boolean flag = stuService.savePayment(payment, stu_id);



        ResultInfo info = new ResultInfo();
        if (flag) {
            info.setFlag(true);
        } else {
            info.setFlag(false);
            info.setErrorMsg("保存缴费记录失败！");
        }
        // 将info序列化为json，并写回到客户端
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(info);
        // 将json写回客户端
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);
    }
    public void stuMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("stuMenu");


        int stu_id = (int) request.getSession().getAttribute("stuid");
        System.out.println(stu_id);

        Student tobechecked = new Student();

        StuDaoImpl stuDao = new StuDaoImpl();

        System.out.println("session" + stu_id);

        Student student;
        tobechecked.setStu_id(stu_id);
        student =  stuDao.findStudentById(tobechecked);

        System.out.println(student.getStu_name());
        System.out.println(student.getStu_sex());
        System.out.println(student.getStu_company());
        System.out.println(student.getStu_post());
        System.out.println(student.getStu_level());
        System.out.println(student.getStu_phone());

        ResultInfo info = new ResultInfo();

        response.setContentType("text/html;charset=utf-8");
        String[] map = {student.getStu_name(), student.getStu_sex(), student.getStu_company(), student.getStu_post(), student.getStu_level(),
                student.getStu_phone()};

        for (String str: map)
            System.out.println(str);

        info.setData(map);
        try {
            writeJsonValue(response, info);
            System.out.println("json回写成功");
        } catch (IOException e) {
            System.out.println("json回写失败");
        }

    }
    public void stuCheckCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("stuCheckCourse");


        int stu_id = (int) request.getSession().getAttribute("stuid");
        System.out.println(stu_id);

        System.out.println("session" + stu_id);

        StuService stuService = new StuServiceImpl();
        List<Map<String, Object>> courses = stuService.findCourses(stu_id);

        for(Map map: courses) {
            System.out.println(map);
            for (Object key:map.keySet()){
                System.out.println(map.get(key).getClass());
                if(key.equals("Cou_time")){
                    System.out.println("change");
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Object o = map.get(key);
                    map.replace(key, df.format(o));
                    break;
                }
                System.out.println(map.get(key).getClass());
            }
        }

        System.out.println(courses);


        ResultInfo info = new ResultInfo();
        info.setData(courses);
        try {
            writeJsonValue(response, info);
            System.out.println("json回写成功");
        } catch (IOException e) {
            System.out.println("json回写失败");
        }

    }

    public void searchCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("searchCourse");

        int stuid = (int) request.getSession().getAttribute("stuid");
        String searchText = request.getParameter("searchText");
        System.out.println(searchText);

        StuService stuService = new StuServiceImpl();
        List<Map<String, Object>> courses = stuService.findCoursesByname(stuid, searchText);

        for(Map map: courses) {
            System.out.println(map);
            for (Object key:map.keySet()){
                System.out.println(map.get(key).getClass());
                if(key.equals("Cou_time")){
                    System.out.println("change");
                    Date date = new Date();
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Object o = map.get(key);
                    map.replace(key, df.format(o));
                    break;
                }
                System.out.println(map.get(key).getClass());
            }
        }


        ResultInfo info = new ResultInfo();
        info.setData(courses);
        try {
            writeJsonValue(response, info);
            System.out.println("json回写成功");
        } catch (IOException e) {
            System.out.println("json回写失败");
        }

    }
    public void searchCoursesNoPaid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("searchCoursesNoPaid");


        int stu_id = (int) request.getSession().getAttribute("stuid");
        System.out.println(stu_id);

        System.out.println("session " + stu_id);

        StuService stuService = new StuServiceImpl();
        List<Map<String, Object>> courses = stuService.findCoursesNotPaid(stu_id);

        for(Map map: courses) {
            System.out.println(map);
            for (Object key:map.keySet()){
                System.out.println(map.get(key).getClass());
                if(key.equals("Cou_time")){
                    System.out.println("change");
                    Date date = new Date();
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Object o = map.get(key);
                    map.replace(key, df.format(o));
                    break;
                }
                System.out.println(map.get(key).getClass());
            }
        }


        ResultInfo info = new ResultInfo();
        info.setData(courses);
        try {
            writeJsonValue(response, info);
            System.out.println("json回写成功");
        } catch (IOException e) {
            System.out.println("json回写失败");
        }

    }
    public void searchByCouNameEX(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("searchByCouNameEX");


        int stu_id = (int) request.getSession().getAttribute("stuid");
        String str = request.getParameter("searchText");
        HttpSession stusession = request.getSession();
        stusession.setAttribute("searchText",str);
        System.out.println(stu_id);

        System.out.println("session" + " "+ str);

        StuService stuService = new StuServiceImpl();
        List<Map<String, Object>> courses = stuService.findCoursesNotPaidEX(stu_id, str);

        for(Map map: courses) {
            System.out.println(map);
            for (Object key:map.keySet()){
                System.out.println(map.get(key).getClass());
                if(key.equals("Cou_time")){
                    System.out.println("change");
                    Date date = new Date();
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Object o = map.get(key);
                    map.replace(key, df.format(o));
                    break;
                }
                System.out.println(map.get(key).getClass());
            }
        }


        ResultInfo info = new ResultInfo();
        info.setData(courses);
        try {
            writeJsonValue(response, info);
            System.out.println("json回写成功");
        } catch (IOException e) {
            System.out.println("json回写失败");
        }

    }
    public void stuCourseNotSigned(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("stuSign");


        int stu_id = (int) request.getSession().getAttribute("stuid");
        System.out.println(stu_id);

        System.out.println("session" + stu_id);

        StuService stuService = new StuServiceImpl();
        List<Map<String, Object>> courses = stuService.findCoursesNotSign(stu_id);

        for(Map map: courses) {
            System.out.println(map);
            for (Object key:map.keySet()){
                System.out.println(map.get(key).getClass());
                if(key.equals("Cou_time")){
                    System.out.println("change");
                    Date date = new Date();
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Object o = map.get(key);
                    map.replace(key, df.format(o));
                    break;
                }
                System.out.println(map.get(key).getClass());
            }
        }


        ResultInfo info = new ResultInfo();
        info.setData(courses);
        try {
            writeJsonValue(response, info);
            System.out.println("json回写成功");
        } catch (IOException e) {
            System.out.println("json回写失败");
        }

    }
    public void stuCourseSign(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("stuSign");


        int stu_id = (int) request.getSession().getAttribute("stuid");
        String count = request.getParameter("count");
        System.out.println(stu_id);

        System.out.println("session" + stu_id);

        StuService stuService = new StuServiceImpl();
        List<Map<String, Object>> courses = stuService.findCoursesNotSign(stu_id);

        Map<String, Object> pay = courses.get(Integer.parseInt(count)-1);

        stuService.saveCourseSign(stu_id, pay);


        ResultInfo info = new ResultInfo();
        info.setData(courses);
        try {
            writeJsonValue(response, info);
            System.out.println("json回写成功");
        } catch (IOException e) {
            System.out.println("json回写失败");
        }

    }
    public void stuAdvice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("stuAdvice");


        int stu_id = (int) request.getSession().getAttribute("stuid");
        System.out.println(stu_id);

        System.out.println("session" + stu_id);

        StuService stuService = new StuServiceImpl();
        List<Map<String, Object>> courses = stuService.findCoursesNotAdvice(stu_id);

        for(Map map: courses) {
            System.out.println(map);
            for (Object key:map.keySet()){
                System.out.println(map.get(key).getClass());
                if(key.equals("Cou_time")){
                    System.out.println("change");
                    Date date = new Date();
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Object o = map.get(key);
                    map.remove(key);
                    map.put(key, df.format(o));
                    break;
                }
                System.out.println(map.get(key).getClass());
            }
        }


        ResultInfo info = new ResultInfo();
        info.setData(courses);
        try {
            writeJsonValue(response, info);
            System.out.println("json回写成功");
        } catch (IOException e) {
            System.out.println("json回写失败");
        }

    }

}
